<?PHP
  $current_page = "profile.php";
  $page_type = "";
  require_once 'header.php';
  
  //The following gigantic IF block is for processing saved information
  if(isset($_POST["SAVED"])){
	for ($i=1; $i<20; $i++){
		$unique_entry = true;
		if(isset($_POST["skill".$i])){
			if($_POST["skill".$i] == NULL) continue;
			$sql_get_user_skill = "select skill from SKILL where UserId='".$_SESSION['user_id']."'";
			$user_skill_result = execute_sql_in_oracle($sql_get_user_skill);
			while ($user_skill = oci_fetch_array($user_skill_result["cursor"])){
				if ($_POST["skill".$i] == $user_skill[0]) $unique_entry = false;
			}
			if ($unique_entry == true){
				$sql = "insert into SKILL (userid, skill) values('".$_SESSION['user_id']."','".$_POST["skill".$i]."')";
				execute_sql_in_oracle($sql);
			}
		}
	}
	$iterations = 0;
	while($iterations < 5000){//This is only set to prevent an infinite loop, the number is arbitrarily high
		if(!isset($_POST["oldskill".$iterations]) || !isset($_POST["oldskilloriginal".$iterations])) break;
		$original = $_POST["oldskilloriginal".$iterations];
		$new = $_POST["oldskill".$iterations];
		if ($original == $new){
			$iterations++;
			continue;
		}
		if ($new == NULL){
			$sql = "delete from SKILL where skill='".$original."' and userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
		else{
			$sql = "update SKILL set skill='".$new."' where userid='".$_SESSION['user_id']."' and skill='".$original."'";
			execute_sql_in_oracle($sql);
		}
		$iterations++;
	}
	if(isset($_POST["DELETE"])){
		if ($_POST["DELETE"] != NULL){
			$to_delete = explode(",",substr($_POST["DELETE"],0,strlen($_POST["DELETE"])-1));//This crazy statement removes the trailing comma before using explode
			forEach($to_delete as $remove_me){
				if(!isset($_POST["oldskilloriginal".$remove_me])) continue;
				$original = $_POST["oldskilloriginal".$remove_me];
				$sql = "delete from SKILL where skill='".$original."' and userid='".$_SESSION['user_id']."'";
				execute_sql_in_oracle($sql);
			}
		}
	}
	if(isset($_POST["fname"])){
		$sql = "select fname from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["fname"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set fname='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["lname"])){
		$sql = "select lname from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["lname"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set lname='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["gender"])){
		$sql = "select gender from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["gender"];
		if ($new != NULL && $new != $original){
			if ($new != "m" && $new != "M" && $new != "f" && $new != "F"){}
			else{
				$sql = "update USER_ACCT set gender='".$new."' where userid='".$_SESSION['user_id']."'";
				execute_sql_in_oracle($sql);
			}
		}
	}
	if(isset($_POST["dob"])){
		$sql = "select dob from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["dob"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set dob='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["street"])){
		$sql = "select street from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["street"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set street='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["city"])){
		$sql = "select city from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["city"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set city='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["state"])){
		$sql = "select state from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["state"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set state='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["zip"])){
		$sql = "select zip from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["zip"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set zip='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["email"])){
		$sql = "select email from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["email"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set email='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["phone"])){
		$sql = "select phone from USER_ACCT where userid='".$_SESSION['user_id']."'";
		$result_array = execute_sql_in_oracle($sql);
		$result = oci_fetch_array($result_array["cursor"]);
		$original = $result[0];
		$new = $_POST["phone"];
		if ($new != NULL && $new != $original){
			$sql = "update USER_ACCT set phone='".$new."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
	}
	if(isset($_POST["password"])){
		if ($_POST["password"] != NULL)
		if (validiatePassword($_POST["password"])){
			$sql = "update USER_ACCT set password='".$_POST["password"]."' where userid='".$_SESSION['user_id']."'";
			execute_sql_in_oracle($sql);
		}
		else{
			$error_message = "Password not Saved, Invalid Password";
		}
		
	}
	
	
	
	if(isset($_POST["monday_start"]) && isset($_POST["monday_end"])){
		if(dayExists("m")) updateTime("m",$_POST["monday_start"],$_POST["monday_end"]);
		else createTime("m",$_POST["monday_start"],$_POST["monday_end"]);
	}
	if(isset($_POST["tuesday_start"]) && isset($_POST["tuesday_end"])){
		if(dayExists("t")) updateTime("t",$_POST["tuesday_start"],$_POST["tuesday_end"]);
		else createTime("t",$_POST["tuesday_start"],$_POST["tuesday_end"]);
	}
	if(isset($_POST["wednesday_start"]) && isset($_POST["wednesday_end"])){
		if(dayExists("w")) updateTime("w",$_POST["wednesday_start"],$_POST["wednesday_end"]);
		else createTime("w",$_POST["wednesday_start"],$_POST["wednesday_end"]);
	}
	if(isset($_POST["thursday_start"]) && isset($_POST["thursday_end"])){
		if(dayExists("tr")) updateTime("tr",$_POST["thursday_start"],$_POST["thursday_end"]);
		else createTime("tr",$_POST["thursday_start"],$_POST["thursday_end"]);
	}
	if(isset($_POST["friday_start"]) && isset($_POST["friday_end"])){
		if(dayExists("f")) updateTime("f",$_POST["friday_start"],$_POST["friday_end"]);
		else createTime("f",$_POST["friday_start"],$_POST["friday_end"]);
	}

  }
  function dayExists($day){
		$sql_get_user_avail = "select * from AVAILABILITY where UserId='".$_SESSION['user_id']."' and day='".$day."'";
		$user_avail_result = execute_sql_in_oracle($sql_get_user_avail);
		$user_avail = oci_fetch_array($user_avail_result["cursor"]);
		if ($user_avail == NULL || !isset($user_avail)) return false;
		else return true;
  }
  function createTime($day,$s_time,$e_time){
		$sql_create = "insert into AVAILABILITY (userid, s_time,e_time, day) values('".$_SESSION['user_id']."',TIMESTAMP'2012-12-01 ".$s_time.":00:00',TIMESTAMP'2012-12-01 ".$e_time.":00:00','".$day."')";
		execute_sql_in_oracle($sql_create);
  }
  function updateTime($day,$s_time,$e_time){
		$sql_update = "update AVAILABILITY set s_time=TIMESTAMP'2012-12-01 ".$s_time.":00:00' where UserId='".$_SESSION['user_id']."' and day='".$day."'";
		$sql_update2 ="update AVAILABILITY set e_time=TIMESTAMP'2012-12-01 ".$e_time.":00:00' where UserId='".$_SESSION['user_id']."' and day='".$day."'";
		execute_sql_in_oracle($sql_update);
		execute_sql_in_oracle($sql_update2);
  }

//BEGIN MAIN PAGE
//BEGIN MAIN PAGE
//BEGIN MAIN PAGE
//BEGIN MAIN PAGE
//BEGIN MAIN PAGE
//BEGIN MAIN PAGE

?>

<style>
	#message{
		color:red;
	}
</style>
<script type="text/javascript">
		var toDelete = [];
        $(document).ready(function() {
			$("#accountInfo").load("UserProfile.php?account=true");
			$("#existingSkills").load("UserProfile.php?skills=true");
			$("#avail").load("UserProfile.php?avail=true");
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
                var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
                // create the new element via clone(), and manipulate it's ID using newNum value
                var newElem = $('#skill' + num).clone().attr('id', 'skill' + newNum);
                // manipulate the name/id values of the input inside the new element
                newElem.children(':first').attr('id', 'skill' + newNum).attr('name', 'skill' + newNum).attr('maxlength', '50');
                // insert the new element after the last "duplicatable" input field
                $('#skill' + num).after(newElem);
                // Only allow up to 20 skills
                if (newNum == 20)
                    $('#btnAdd').prop('disabled','disabled');
            });
        });
    </script>
	<div id="page">
	  <div id="page-padding">
        <!-- start content -->	    
	    <div id="content">
	      <div id="content-padding">
            <h1>User Profile</h1>
            <div id="message" align="center"><?php if(isset($_POST["SAVED"])) echo "Data saved"; if(isset($error_message)) echo " - ".$error_message;?></div>
			<form id="myForm" action="profile.php" method="POST">
				<div id="accountInfo" style="margin-bottom:4px;">
				</div>
				<div id="skill1" style="margin-bottom:4px;" class="clonedInput">
					New Skill: <input type="text" name="skill1" id="skill1" maxlength="50" />
				</div>
				<div id="existingSkills" style="margin-bottom:4px;">
				</div>
				<div id="avail" style="margin-bottom:4px;">
				</div>
				<div>
					<input type="hidden" id="DELETE" name="DELETE" value="" />
					<input type="hidden" id="SAVED" name="SAVED" value="1" />
					<input type="button" id="btnAdd" value="Add Another Skill" />
					<input type="button" id="btnSave" onclick="submitData()" value="Save" />
				</div>
				
			</form>
			
            <p>&nbsp; </p>
			
	      </div>
		</div>
	  <!-- end content --></div>
	  <div id="footer">
	    <div id="footer-pad">
	      <div class="line"></div>
		  <!-- footer and copyright notice -->
	      <p>Design by <a href="http://www.steves-templates.com">Steve's free website templates</a>.</p>
		  <!-- end footer and copyright notice -->
	    </div>
	  </div>
	</div>
  </body>
</html>
<script>
function submitData(){
	//Process deleting items here
	$("#myForm").submit();
}
function deleteSkill(num){
	$('#oldskilllabel' + num).remove();
	$('#oldskill' + num).remove();
	$('#btnRemove' + num).remove();
	$('#DELETE').val($('#DELETE').val() + num + ",");
}
function apply(){
	if(confirm('Are you sure you want to apply for stakeholder status?')){
		$("#message").load("apply.php");
		$("#accountInfo").load("UserProfile.php?account=true");
	}
}
</script>